<template>
  <div class="upload">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-imgerror="imgerror" :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <span v-if="value">
      <i class="el-icon-zoom-in" @click="look" />
      <i class="el-icon-delete" @click="del" />
    </span>
    <el-dialog :visible.sync="isShow" width="700px" :show-close="false">
      <img width="100%" :src="value" alt="">
    </el-dialog>
    <transition name="progress">
      <el-progress v-if="showProgress" :percentage="percentage" />
    </transition>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDWrwlKdbNs75mBOM4VgbFciY8RUHo43lE',
  SecretKey: 'EBrHQBgwwgANJyMTJxMUMxHK7dsreAPN'
})
export default {
  name: 'ImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isShow: false,
      showProgress: false,
      percentage: 0,
      imgerror: require('@/assets/common/bigUserHeader.png')
    }
  },
  methods: {
    // 上传前
    beforeAvatarUpload(file) {
      const isType = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isType) {
        this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isType && isLt2M
    },
    // 自定义上传
    httpRequest(data) {
      this.showProgress = true
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'xy961234-1308138773', // 存储桶
          Region: 'ap-guangzhou', // 存储桶所在地域，必须字段
          Key: data.file.name, // 标识
          StorageClass: 'STANDARD', // 固定值
          Body: data.file, // 上传文件对象  file文件对象
          onProgress: (progressData) => {
            // 上传进度，实时返回上传文件进度
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showProgress = false
              }, 1000)
            }
          }
        },
        (err, data) => {
          // 上传成功或者失败处理
          // err为空就是成功，err有值就是失败了
          // data就是上传成功后腾讯云返回的数据（上传文件地址）
          if (!err) {
            this.$emit('input', 'http://' + data.Location)
          }
        }
      )
    },
    // 放大头像点击
    look() {
      this.isShow = true
    },
    // 删除头像点击
    del() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.upload {
  width: 178px;
  height: 178px;
  &:hover {
    span {
      display: block;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
      background: rgba(0, 0, 0, 0.5);
    }
  }
  span {
    display: none;
    position: absolute;
    left: 0;
    top: 0;
    border-radius: 6px;
    font-size: 22px;
    i {
      padding: 0 5px;
      cursor: pointer;
      color: #fff;
      &:hover {
        color: #409eff;
      }
    }
  }
}
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
::v-deep .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.progress-enter-active,
.progress-leave-active {
  transition: all 0.5s;
}
.progress-enter,
.progress-leave-to {
  opacity: 0;
}
</style>
